package com.pym.service.impl;

import com.pym.dao.WpxxDao;
import com.pym.entity.Wpxx;
import com.pym.service.WpglService;
import com.pym.util.Common_ExcelUtil;
import com.pym.util.TimeTools;
import com.pym.util.TypeToNameUtil;
import com.pym.vo.WpxxVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author Jerry
 * @Description //TODO 物品管理接口实现类
 * @Date 15:47 2021/8/16
 **/
@Service("wpxxService")
public class WpglServiceImpl implements WpglService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WpxxDao wpxxDao;

    /**
     * @Author Jerry
     * @Description //TODO 根据查询条件查询数据集合
     * @Date 16:24 2021/8/16
     * @param wpxx 查询条件实体
     * @param startDate 起始时间
     * @param endDate 结束时间
     * @return List<Wpxx> 物品集合
     **/
    @Override
    public List<Wpxx> getAll(Wpxx wpxx, String startDate, String endDate) {
        List<Wpxx> wpxxList = this.wpxxDao.queryAll(wpxx, startDate, endDate);
        for (Wpxx wpxx1 : wpxxList) {
            // 将状态值转换成状态名
            wpxx1.setWpztName(TypeToNameUtil.getWpztName(wpxx1.getWpzt()));
        }
        return wpxxList;
    }

    /**
     * @Author Jerry
     * @Description //TODO 通过主键更新单条数据
     * @Date 14:57 2021/8/17
     * @Param [wpxx] 更新物品信息实体
     * @return 返回受影响行数
     **/
    @Override
    public int updateWpxxById(Wpxx wpxx) {
        return this.wpxxDao.update(wpxx);
    }

    /**
     * @Author Jerry
     * @Description //TODO 通过主键删除单条数据
     * @Date 15:08 2021/8/17
     * @Param [id] 物品信息主键ID
     * @return 返回受影响行数
     **/
    @Override
    public int deleteWpxxById(String id) {
        if(id.contains(",")){
            String[] idArray = id.split(",");
            return this.wpxxDao.batchDeleteById(idArray);
        }
        return this.wpxxDao.deleteById(id);
    }

    /**
     * @Author Jerry
     * @Description //TODO 导出物品信息表（Excel）
     * @Date 17:26 2021/8/18
     * @Param [response 内置参数, wpxx 查询条件实体, registerDate 查询日期]
     **/
    @Override
    public void exportWpxx(HttpServletResponse response, Wpxx wpxx, String startDate, String endDate) throws IOException {
        List<WpxxVo> wpxxVolist = this.wpxxDao.queryWpxxForExport(wpxx, startDate, endDate);

        String clos[] = {"序号","物品编号","物品名称","持有人","数量","来源","状态","案件编号","案件名称","承办单位","主办民警","登记时间","特征","存放位置","出库时间","入库时间","处置时间","物品缩略图"};
        String keys[] = {"sort","wpbh","wpmc","cyrXm","wpsl","wply","wpztName","ajbh","ajmc","cbdw","zbmjXm","addTime","wptz","cfwz","ckTime","rkTime","czTime","pictureUrl"};

        int i = 1;
        for (WpxxVo wpxxVo : wpxxVolist) {
            // 设置序号
            wpxxVo.setSort(i);
            // 将状态值转换成状态名
            wpxxVo.setWpztName(TypeToNameUtil.getWpztName(wpxxVo.getWpzt()));
            i++;
        }
        // 工具类实现导出Excel文件
        Common_ExcelUtil.downexcle(response, wpxxVolist, clos, keys, TimeTools.getTimeStamp());
    }

    /**
     * 根据主键查询物品信息接口
     * @param id 物品ID
     * @return WpxxVo
     */
    @Override
    public WpxxVo queryWpxxById(String id) {
        WpxxVo wpxxVo = this.wpxxDao.queryById(id);
        if(wpxxVo != null){
            // 显示物品状态名称
            wpxxVo.setWpztName(TypeToNameUtil.getWpztName(wpxxVo.getWpzt()));
        }
        return wpxxVo;
    }

    @Override
    public String queryWpztById(String id) {
        return this.wpxxDao.queryWpztById(id);
    }

    @Override
    public int updateWpxxWpztById(String id, String exeOperStatus, String addTime) {
        return this.wpxxDao.updateWpxxWpztById(id, exeOperStatus, addTime);
    }
}
